#!/bin/bash
extlib_count () {
	loc=`dirname $0`/DOPEN/extlib.ded
	echo `wc -l $loc`
}
extupdate () {
	crl=`curl http://d-cm.googlecode.com/files/extlib.ded | grep @DCM:VERSION`
	version=${crl#*|}
	locl="./DOPEN/extlib.ded"
	for name in $(<$locl)
	do
		e=$e"\n"$name
	done
	for line in `echo -e $e`
	do
		if [[ $line == @DCM:VERSION* ]]
		then
			local=${line#*|}
		fi
	done
	if [[ $version > $local ]]
	then
		echo "notuptodate"
	    echo $version
	    echo $local
	else
		echo "uptodate"
	fi
}
status () {
	if [[ `ps ax | grep -v grep | grep dcm$` ]]
	then
		echo "dcm is running"
	else
		echo "dcm is not running"
	fi
}
update () {
    latest=`curl http://d-cm.googlecode.com/files/latest_version`
    locl="./config.ded"
    for name in $(<$locl)
    do
        e=$e"\n"$name
    done
    for line in `echo -e $e`
    do
        if [[ $line == @DCM:VERSION* ]]
        then
            local=${line#*|}
        fi
    done
    if [[ $latest > $local ]]
    then
        crl=`curl http://d-cm.googlecode.com/files/d-cm_v$latest.tar.gz -o d-cm_v$latest.tar.gz`
        echo "d-cm_v$latest.tar.gz downloaded"
        if [[ -d ./backups/ ]]
        then
            echo "backup dir found"
        else
            echo "backup dir not found"
            mkdir ./backups/
            echo "backup dir created"
        fi
        rsync -r --exclude='backups' ./ ./backups
        echo "All files except the tar.gz copied to ./backups"
        tar xvzf d-cm_v$latest.tar.gz
        echo "All files extracted"
        rm d-cm_v$latest.tar.gz
        rm ./backups/d-cm_v$latest.tar.gz
        echo "D-CM is upgraded to version $latest"
    else
        echo "D-CM is already the newest version"
    fi
}
while getopts esuU opt
do
    case "$opt" in
      e)  extlib_count ;;
      s)  status ;;
      u)  extupdate ;;
      U)  update ;;
      \?) echo >&2 \
	  "usage: $0 [-e] [-s] [-u] [-U]"
	  exit 1;;
    esac
done
echo "usage: $0 [-e] [-s]"
echo "-e: Get the number of lines in extlib"
echo "-s: Status"
echo "-u: update extlib"
echo "-U: Update D-CM"
shift `expr $OPTIND - 1`
